<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="商圈名称">
              <a-input v-model="queryParam.name" placeholder="请输入"/>
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="24">
            <a-form-item label="上级机构">
              <a-tree-select
                :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                placeholder='请选择'
                allowClear
                treeDefaultExpandAll
                :treeData="treeData"
                v-model="queryParam.code"
              >
              <span style="color:#08c" slot="title" slot-scope="{key}"
                    v-decorator="['key',{rules: [{ required: true, message: '请选择商户' }]}]">
                {{value}}
              </span>>
              </a-tree-select>
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button v-action:query type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button v-action:query style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button v-action:add type="primary" icon="plus" @click="$refs.modal.add()">新建</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="{ show: true, clear: true }"
      :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
    >
      <span slot="state" slot-scope="text">
        {{ text | statusFilter }}
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-action:update @click="$refs.modal.edit(record)">编辑</a>
          <a-divider type="vertical"/>
<!--          <a v-action:update @click="$refs.modal1.edit(record)">图片配置</a>-->
<!--          <a-divider type="vertical"/>-->
<!--          <a v-action:delete @click="handleDel(record)">删除</a>-->
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a v-action:update @click="$refs.modal1.edit(record)">图片配置</a>
             </a-menu-item>
              <!--<a-menu-item>
                <a v-action:update @click="$refs.modal2.edit(record)">营销配置</a>
             </a-menu-item>-->
            <a-menu-item>
              <a v-action:delete @click="handleDel(record)">删除</a>
            </a-menu-item>
          </a-menu>
          </a-dropdown>
        </template>
      </span>
    </s-table>

    <Edit ref="modal" @ok="handleOk"></Edit>
    <EditPic ref="modal1" @ok="handleOk"></EditPic>
    <Config ref="modal2" @ok="handleOk" ></Config>


  </a-card>
</template>

<script>
  import STable from '@/components/table/'
  import Edit from './Edit'
  import EditPic from './EditPic'
  import Config from './Config'
  import { selectList, del} from '@/api/companyService/company'
  import { selectTree } from '@/api/storeService/store'

  export default {
    components: {
      STable,
      Edit,
      EditPic,
      Config
    },
    //--############  data() ###############-->
    data() {
      return {
        description: '',
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '编号',
            dataIndex: 'code'
          },
          {
            title: '服务商名称',
            dataIndex: 'businessServiceName'
          },
          {
            title: '商圈名称',
            dataIndex: 'name'
          },

          {
            title: '小程序appid',
            dataIndex: 'miniAppid'
          },
          {
            title: '操作',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return selectList(Object.assign(parameter, this.queryParam))
            .then(res => {
              console.log('list',res)
              return res.result
            })
        },

        selectedRowKeys: [],
        selectedRows: [],
        treeData:[]
      }
    },
    //--############  data() ###############-->
    created() {
      const _this = this
      const params = { 'ctrlType': '1', 'displayType': '2' }
      /*ctrlType:控制层级结构不能操作，1，服务商，2，代理商，3商户，4，门店
      * displayType:控制层级显示，必须是连续，1，服务商，2，代理商，3商户，4，门店
      * */
      selectTree(params).then(res => {
        if (res.code === 200) {
          if (res.result == null) {
            return
          }
          _this.treeData = res.result
          console.log('0000',_this.treeData)
        } else {
          this.$notification.error({
            message: '错误',
            description: res.message,
            duration: 4
          })
        }
      })
    },
    //--############  methods() ###############-->
    methods: {
      handleDel(row) {
        const _this = this
        this.$confirm({
          title: '警告',
          content: `真的要删除 ${row.name} 吗?`,
          okText: '删除',
          okType: 'danger',
          cancelText: '取消',
          onOk() {
            // 在这里调用删除接口
            del(row.id).then(res => {
              console.log('删除',res)
              _this.$message.info(res.message)
              _this.$refs.table.refresh()
            })
          },
          onCancel() {
            console.log('Cancel')
          }
        })
      },
      handleOk() {
        // 新增/修改 成功时，重载列表
        this.$refs.table.refresh(true)
      },

      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      toggleAdvanced() {
        this.advanced = !this.advanced
      }
    },
    //--############  methods() ###############-->
    //--############  filters() ###############-->
    filters: {
      statusFilter(state) {
        const statusMap = {
          false: '未使用',
          true: '已使用'
        }
        return statusMap[state]
      }
    }
    //--############  filters() ###############-->
  }
</script>
